package com.msteg.b4a.app.adapters;

import java.util.List;

import org.witness.informacam.InformaCam;
import org.witness.informacam.models.forms.IForm;
import org.witness.informacam.models.media.ILog;
import org.witness.informacam.models.media.IMedia;
import org.witness.informacam.utils.Constants.App;
import org.witness.informacam.utils.Constants.App.Storage.Type;
import org.witness.informacam.utils.TimeUtility;

import com.msteg.b4a.Constants.MainActivityListener;
import com.msteg.b4a.R;
import com.msteg.b4a.Constants.Form;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ObservationListAdapter extends BaseExpandableListAdapter {
	List<ILog> observations;
	InformaCam informaCam;

	public ObservationListAdapter(List<ILog> observations) {
		this.observations = observations;
		informaCam = InformaCam.getInstance();
	}


	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return observations.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(informaCam.a).inflate(R.layout.adapters_observation_list_child_view, null);

		TextView attachedMediaDetails = (TextView) convertView.findViewById(R.id.observation_attached_media_label);
		LinearLayout attachedMedia = (LinearLayout) convertView.findViewById(R.id.observation_attached_media_thumbnail_holder);

		final ILog observation = observations.get(groupPosition);

		if(observation.attachedMedia.size() == 0) {
			attachedMediaDetails.setText(informaCam.a.getString(R.string.no_attached_media));
		} else {
			attachedMediaDetails.setText(informaCam.a.getString(R.string.x_media_attached, observation.attachedMedia.size()));

			for(String s : observation.attachedMedia) {
				IMedia m = informaCam.mediaManifest.getById(s);
				byte[] thumbBytes = informaCam.ioService.getBytes(m.bitmapThumb, Type.IOCIPHER);

				Bitmap b = BitmapFactory.decodeByteArray(thumbBytes, 0, thumbBytes.length);

				ImageView iv = new ImageView(informaCam.a);

				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(90, 90);
				lp.setMargins(4, 0, 4, 0);

				iv.setLayoutParams(lp);
				iv.setImageBitmap(b);

				attachedMedia.addView(iv);
			}
		}

		Button edit = (Button) convertView.findViewById(R.id.observation_edit);
		edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((MainActivityListener) informaCam.a).editLog(observation);
			}

		});
		
		final ProgressBar deleteWaiter = (ProgressBar) convertView.findViewById(R.id.observation_delete_waiter);

		Button delete = (Button) convertView.findViewById(R.id.observation_delete);
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				(new Handler()).post(new Runnable() {

					@Override
					public void run() {
						deleteWaiter.setVisibility(View.VISIBLE);
						((Button) v).setVisibility(View.GONE);
					}
				});
				
				((MainActivityListener) informaCam.a).deleteLog(observation);
			}

		});

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return observations.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return observations.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(informaCam.a).inflate(R.layout.adapters_observation_list_view, null);

		ILog observation = observations.get(groupPosition);

		TextView observationTimestamp = (TextView) convertView.findViewById(R.id.observation_timestamp);
		observationTimestamp.setText(TimeUtility.millisecondsToDatestamp(observation.startTime));

		EditText observationContent = (EditText) convertView.findViewById(R.id.observation_content);

		Log.d(App.LOG, "getting answers for " + observation.asJson().toString());
		
		byte[] answerBytes = null;
		if(observation.formPath != null) {
			answerBytes = InformaCam.getInstance().ioService.getBytes(observation.formPath, Type.IOCIPHER);
			IForm form = new IForm(observation.attachedForm, informaCam.a, answerBytes);
			form.associate(observationContent, Form.CONTENT);

			TextView observationContentDisplay = (TextView) convertView.findViewById(R.id.observation_content_display);
			observationContentDisplay.setText(observationContent.getText().toString());
		}

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
